/*
 * Inputs ADC Value from Thermistor and outputs Temperature in Celsius
 *  requires: include <math.h>
 * Utilizes the Steinhart-Hart Thermistor Equation:
 *    Temperature in Kelvin = 1 / {A + B[ln(R)] + C[ln(R)]3}
 *    where A = 0.001129148, B = 0.000234125 and C = 8.76741E-08
 *
 * These coefficients seem to work fairly universally, which is a bit of a 
 * surprise. 
 *
 * Schematic:
 *   [Ground] -- [10k-pad-resistor] -- | -- [thermistor] --[Vcc (5 or 3.3v)]
 *                                     |
 *                                Analog Pin 0
 *
 * In case it isn't obvious (as it wasn't to me until I thought about it), the analog ports
 * measure the voltage between 0v -> Vcc which for an Arduino is a nominal 5v, but for (say) 
 * a JeeNode, is a nominal 3.3v.
 *
 * The resistance calculation uses the ratio of the two resistors, so the voltage
 * specified above is really only required for the debugging that is commented out below
 *
 * Resistance = (1024 * PadResistance/ADC) - PadResistor 
 *
 * I have used this successfully with some CH Pipe Sensors (http://www.atcsemitec.co.uk/pdfdocs/ch.pdf)
 * which be obtained from http://www.rapidonline.co.uk.
 *
 // brewcoder credits to http://arduino.cc/playground/ComponentLib/Thermistor2
 */

#include <math.h>
//#include "global.h"

//#define ThermistorPIN 0                 // Analog Pin 0

float vcc = 4.91;                       // only used for display purposes, if used
// set to the measured Vcc.
float pad = 9850;                       // balance/pad resistor value, set this to
// the measured resistance of your pad resistor
float thermr = 10000;                   // thermistor nominal resistance




long Thermistor(int RawADC) {
  float Resistance;  
  float Temp;  // Dual-Purpose variable to save space.

  Resistance=((1024 * pad / RawADC) - pad); 
  Temp = log(Resistance); // Saving the Log(resistance) so not to calculate  it 4 times later
  Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
  Temp = Temp - 273.15;  // Convert Kelvin to Celsius                      
  //Serial.print("\nADC: "); 
  //  Serial.print(RawADC); 
  // Uncomment this line for the function to return Fahrenheit instead.
  //temp = (Temp * 9.0)/ 5.0 + 32.0;                  // Convert to Fahrenheit
  return (long)(Temp*100);                                      // Return the Temperature
}

void sampleAllTemps(){
  if(millis() - prevMillis_Temp >= SampleRefreshTemp){
    prevMillis_Temp = millis(); 
    fridgeTemp1 = Thermistor(filterTempResults(TEMP_INPUT_FRIDGE)); 
   
  }
}

long filterTempResults(int pin){

  long tempArry[A2D_SAMPLES];
  long arrayTotal = 0;
  int j = 0;

  for(int x = 0; x < A2D_SAMPLES; x++){
    int currentA2D = analogRead(pin);

    // Find postion to incert value
    for(int y = 0; y <= x; y++){ 
      j= y;
      if(tempArry[y] >= currentA2D || tempArry[y] == 0){
        // incert at postion y
        break;
      }
    }
    // Shift array right
    for(int m = x; m >=j; m--){
      tempArry[m+1] = tempArry[m]; 
    }
    tempArry[j] = currentA2D;
  }

  return tempArry[A2D_SAMPLES/2];  
}



